// pages/order/order.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      list:[],
      active:0,
      list1:[],
      list2:[],
      list3:[],
      list4:[],
      transMes:null,
      showtrans:false,//物流信息弹框
      page:0,//分页
      limit:5,//每页多少条
      cancelReason: [{ title: '不取消了' }, { title: '不想买了' }, { title: '拍错了' }, { title: '其他' }],
      showCancel:false,//取消订单原因
      cancelReOn: 0,//取消原因
      cancelId:'',//取消订单的id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let data = {};
      let s=options.s;
      this.setData({ active:options.s});
      if(options.s==0){
          data={
            limit: this.data.limit,
            offset: 0
          }
      }else if(options.s==1){
        data = {
          'status[0]': 0,
          'status[1]': 1,
          limit: this.data.limit,
          offset: 0
        }
      }else{
        data = {
          status:options.s,
          limit: this.data.limit,
          offset: 0
        }
      }
      
    let self = this;
    
    
    wx.request({
      url: app.globalData.reqPath + 'shop/orders',
      header: {
        'authorization': app.globalData.authorization
      },
      data: data,
      success: function ({ data }) {
        if (data.code == 200) {
          let list = data.data;
            if(s==0){
              self.setData({ list:list})
            }else if(s==1){
              self.setData({ list1:list})
            }else if(s==2){
              self.setData({ list2:list})
            }else if(s==3){
              self.setData({ list3:list})
            }else if(s==4){
              self.setData({ list4:list})
            }
         
        }
      }
    })
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.active==0){
      this.getMore()
    }else{
      this.getMore2()
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  cancelorder:function(e){
    let id=e.currentTarget.dataset.id;
    let self=this;
    this.setData({ showCancel: true, cancelId:id});
    // wx.showModal({
    //   content: '确定取消订单？',
    //   success: function (res) {
    //     if (res.confirm) {
    //       //console.log('用户点击确定')
    //       wx.request({
    //         url: app.globalData.reqPath + 'shop/OrdersStatusCancle',
    //         method: 'POST',
    //         header: {
    //           authorization: app.globalData.authorization
    //         },
    //         data: {
    //           orderId: id
    //         },
    //         success: function ({ data }) {
    //           if (data.code == 200) {
    //             wx.showToast({
    //               title: '取消成功',
    //               icon: 'none',
    //               success: function () {
    //                 self.onLoad();
    //               }
    //             })
    //           }
    //         }
    //       })
    //    }
    //   }
    // })
  },
  confrimPay:function(e){
    let id=e.currentTarget.dataset.id

    console.log(id)
    wx.redirectTo({
      url:'/pages/orderDetail/orderDetail?id='+id  
    })
  },
  return1:function(e){//申请退款
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/return/return?id='+id+'&type=2',
    })
  },
  checkTransMes:function(e){//查看物流
      let id=e.currentTarget.dataset.id;
      let self=this;
    this.setData({showtrans:true})
      wx.request({
        url:app.globalData.reqPath+'shop/express',
        data:{
          orderId:id
        },
        header: {
          authorization: app.globalData.authorization
        },
        success:function({data}){
            if(data.code==200){
                self.setData({transMes:data.data})
            }
        }
      })

  },
  onClosetrans:function(){
    this.setData({ showtrans: false })
  },
  return2:function(e){//申请退货
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/return/return?id=' + id + '&type=1',
    })
  },
  cfmget:function(e){
    let id = e.currentTarget.dataset.id;
    let self=this;
    wx.showModal({
      title: '是否确认收到货？',
      success:function(res){
          if(res.confirm){
            wx.request({
              url: app.globalData.reqPath + 'shop/OrdersStatusExpress',
              method: 'POST',
              data: {
                orderId: id
              },
              header: {
                'authorization': app.globalData.authorization
              },
              success: function ({ data }) {
                if (data.code == 200) {
                  self.onLoad({ s: 3 })
                }
              }
            })
          }
      }
    })

  },
  torate:function(e){//去评价
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/subrate/subrate?id='+id,
    })

  },
  goDetail:function(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/orderDetail/orderDetail?id='+id,
    })
  },
  getMore:function(){//加载更多（全部）
    let self = this;
    wx.request({
      url: app.globalData.reqPath + 'shop/orders',
      header: {
        'authorization': app.globalData.authorization
      },
      data: {
        limit: this.data.limit,
        offset: ++this.data.page * this.data.limit
      },
      success: function ({ data }) {
        if (data.code == 200) {
          let list = data.data;
          if(list.length>0){
            self.setData({ list:self.data.list.concat(list) });
          }else{

          }
        }
      }
    })
  },
  changetab:function(e){
    let index=e.detail.index;
    this.setData({active:index,page:0})
    this.getStatusList(index);   
  },
  getStatusList:function(s){//切换标签时获取对应订单列表
  let self=this;
  let data=null;
  if(s==0){
    data={
      limit: this.data.limit,
      offset: 0
    }
  }else
  if(s==1){
    data = {
      'status[0]': 0,
      'status[1]':1,
      limit: this.data.limit,
      offset: 0
    }
  }else{
    data = {
      status:s,
      limit: this.data.limit,
      offset: 0
    }
  }

    wx.request({
      url: app.globalData.reqPath+'shop/orders',
      data:data,
      header:{
        'authorization': app.globalData.authorization
      },
      success:function({data}){
          if(data.code==200){
            if(s==0){
              self.setData({list: data.data })
            }else
            if(s==1){
              self.setData({list1:data.data})
            }else if(s==2){
              self.setData({list2:data.data})
            }else if(s==3){
              self.setData({list3:data.data})
            }else if(s==4){
              self.setData({list4:data.data})
            }
          }
      }
    })
  },
  getMore2:function(){//加载更多
    let s=this.data.active;
    let self=this;
    let data={};
    if(s==1){
      data={
        'status[0]': 0,
        'status[1]': 1,
        limit: this.data.limit,
        offset: ++this.data.page * this.data.limit
      }
    }else{
      data = {
        status:s,
        limit: this.data.limit,
        offset: ++this.data.page * this.data.limit
      }
    }

    wx.request({
      url: app.globalData.reqPath + 'shop/orders',
      data: data,
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data }) {
        if (data.code == 200) {
          let list=data.data;
            if (s == 1) {
              self.setData({ list1: self.data.list1.concat(list)})
            } else if (s == 2) {
              self.setData({ list2: self.data.list2.concat(list)})
            } else if (s == 3) {
              self.setData({ list3: self.data.list3.concat(list)})
            } else if (s == 4) {
              self.setData({ list4: self.data.list4.concat(list)})
            }
        }
      }
    })



  },
  onClosecancel:function(e){
    this.setData({ showCancel: false })
  },
  changecancel: function (e) {//选择取消原因
    // console.log(e)
    let resonindex = e.detail.value;
    let self = this;
    this.setData({ cancelReOn: resonindex });
    if (resonindex == 0) {
      this.setData({ showCancel: false })
    } else {
      wx.showModal({
        content: '确定取消订单？',
        success: function (res) {
          if (res.confirm) {
            //console.log('用户点击确定')
            wx.request({
              url: app.globalData.reqPath + 'shop/OrdersStatusCancle',
              method: 'POST',
              header: {
                authorization: app.globalData.authorization
              },
              data: {
                orderId: self.data.cancelId,
                reson: self.data.cancelReason[self.data.cancelReOn]
              },
              success: function ({ data }) {
                if (data.code == 200) {
                  wx.showToast({
                    title: '取消成功',
                    icon: 'none',
                    success: function () {
                      self.setData({ showCancel: false })
                      self.onLoad({s:0});
                    }
                  })
                }else{
                  wx.showToast({
                    title: data.data.msg,
                    icon:'none'
                  })
                }
              }
            })
          } else if (res.cancel) {
            self.setData({ showCancel: false })
          }

        }
      })
    }
  },
})